package ils.services;

import ils.domain.acquisition.model.Delivery;
import ils.domain.acquisition.model.DeliveryItem;
import ils.domain.acquisition.model.Order;
import ils.domain.acquisition.model.OrderFactory;
import ils.domain.acquisition.model.OrderItem;
import ils.domain.acquisition.model.Seller;
import ils.domain.acquisition.model.SellerFactory;
import ils.util.Util;
import ils.web.model.WDelivery;
import ils.web.model.WDeliveryItem;
import ils.web.model.WOrder;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AquisitionCommonImpl implements AquisitionCommon
{
    private SellerFactory sellerFactory;
    private OrderFactory orderFactory;
    @Override
    public List<WDelivery> convertDeliveries(List<Delivery> deliveries)
    {
        List<WDelivery> wDeliveries = new ArrayList<WDelivery>();
        List<Integer> sellerIds = new ArrayList<Integer>();
        for(Delivery delivery : deliveries)
        {
            WDelivery wDelivery = new WDelivery();
            Util.copy(wDelivery, delivery);
            int sellerId = delivery.getSeller().getId();
            sellerIds.add(sellerId);
            wDelivery.setSellerId(sellerId);
            wDeliveries.add(wDelivery);
        }
        Map<Integer, Seller> sellers = sellerFactory.getSellers(sellerIds);
        for(WDelivery wDelivery : wDeliveries)
        {
            Seller seller = sellers.get(wDelivery.getSellerId());
            wDelivery.setSellerName(seller.getName());
        }
        return wDeliveries;
    }

    @Override
    public List<WOrder> convertOrders(List<Order> orders)
    {
        List<WOrder> worders = new ArrayList<WOrder>();
        List<Integer> sellerIds = new ArrayList<Integer>();
        for(Order order : orders)
        {
            WOrder wOrder = new WOrder();
            Util.copy(wOrder, order);
            int sellerId = order.getSeller().getId();
            sellerIds.add(sellerId);
            wOrder.setSellerId(sellerId);
            worders.add(wOrder);
        }
        Map<Integer, Seller> sellers = sellerFactory.getSellers(sellerIds);
        for(WOrder wOrder : worders)
        {
            String sellerName = sellers.get(wOrder.getSellerId()).getName();
            wOrder.setSellerName(sellerName);
        }
        return worders;
    }

    @Override
    public Set<DeliveryItem> convertToDeliveryItems(List<WDeliveryItem> items)
    {
        List<Integer> orderItemIds = new ArrayList<Integer>();
        for(WDeliveryItem item : items)
        {
            orderItemIds.add(item.getOrderItemId());
        }
        Map<Integer, OrderItem> orderItems = orderFactory.getItems(orderItemIds);
        Set<DeliveryItem> deliveryItems = new HashSet<DeliveryItem>();
        for(WDeliveryItem item : items)
        {
            OrderItem orderItem = orderItems.get(item.getOrderItemId());
            item.setOrderItem(orderItem);
            deliveryItems.add(item);
        }
        return deliveryItems;
    }

    public void setOrderFactory(OrderFactory orderFactory)
    {
        this.orderFactory = orderFactory;
    }

}
